// pages/promotion/promotion.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    promotion:[],
    choose:[],
    price:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      navH: app.globalData.navHeight,
      priceList:options.priceList,
      productList:options.productList,
      numList:options.numList,
      promotionId:options.promotionId
    });
    var productIdList = that.data.productList.split(",");
    var priceList = that.data.priceList.split(",");
    var numList = that.data.numList.split(",");
    var productListJson = "[";
      for(var i=0; i < productIdList.length;i++){
        productListJson+="{productId:"+productIdList[i]+",num:"+numList[i]+",price:"+priceList[i]+"},"
        
      }
      productListJson = productListJson.substring(0,productListJson.length-1);
      productListJson+="]";
    wx.request({
      url: app.globalData.url + 'getActivityPromotionByProductId',
      method:"get",
      async: false ,
      data:{
        productList: productListJson,
      },
      success:function(res){
        if(res.data.result == 1){
          that.setData({
            promotion:res.data.data,
          })
          var chooseList = [];
          chooseList.length = that.data.promotion.length;
          for (var i = 0; i < chooseList.length; i++){
            if (that.data.promotionId == that.data.promotion[i].activityId) {
              chooseList[i] = true;
            }else {
              chooseList[i] = false;
            }
          }
          that.setData({
            choose: chooseList,
          });
        } else {
          wx.showToast({
            title: res.data.errmsg,
            icon:"none",
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //选中活动
  choose:function(e){
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index);
    var list = that.data.choose;
    that.setData({
      price:0,
      ind:-1
    })
    if(list[index] == true){
      list[index] = false;
      that.setData({
        choose: list,
        promotionId: 0,
        promotionTitle: "",
      })
      return;
    }
    if(list[index]){
      index = -1;
      that.setData({
        promotionId: 0,
        promotionTitle: '',
      })
    }
    // var productIdList = that.data.productList.split(",");
    // var productPriceList = that.data.priceList.split(",");
    //   for(var i=0;i<productIdList.length;i++){
    //     wx.request({
    //       url: app.globalData.url + 'getActivityPromotionByProductId',
    //       method:"get",
    //       data:{
    //         productList: productIdList[i]+"",
    //       },
    //       success:function(res){
    //         var ind = that.data.ind+1;
    //         that.setData({
    //             ind:ind
    //         })
    //         if(res.data.result == 1){
    //           for(var j=0;j<res.data.data.length;j++){
    //             console.log(that.data.promotion[index].activityId+"..."+res.data.data[j].activityId)
    //               if(that.data.promotion[index].activityId == res.data.data[j].activityId){
    //                   that.data.price += parseFloat(productPriceList[that.data.ind]);
    //                   that.setData({
    //                     price:that.data.price
    //                   })
    //                   console.log(that.data.price+"!!!"+that.data.promotion[index].requirementPrice)
    //                   if((that.data.ind+1) == productIdList.length&&(j+1) == res.data.data.length){
    //                     console.log(that.data.price+"+++"+that.data.promotion[index].requirementPrice)
    //                     if(that.data.price >= that.data.promotion[index].requirementPrice){
    //                         for (var i = 0; i < list.length; i++){
    //                           list[i] = false;
    //                           if(i == index){
    //                             list[index] = true;
    //                           }
    //                         }
    //                         that.setData({
    //                           choose:list,
    //                         });
    //                         that.setData({
    //                           promotionId: that.data.promotion[index].activityId,
    //                           promotionTitle: that.data.promotion[index].describe,
    //                         })
                          
    //                     }else{
    //                         wx.showToast({
    //                           title: '不满足条件',
    //                           icon:"none"
    //                         })
    //                     }
    //                   }
    //               }
                  
    //           }
             
    //         }
    //       }
    //     })
       
    //   }
for (var i = 0; i < list.length; i++){
    list[i] = false;
    if(i == index){
      list[index] = true;
    }
  }
  that.setData({
    choose:list,
  });
  that.setData({
    promotionId: that.data.promotion[index].activityId,
    promotionTitle: that.data.promotion[index].describe,
  })
    
  },

  navBack: function () {
    var that = this;
    let pages = getCurrentPages();  // 当前页的数据，可以输出来看看有什么东西
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      promotionId: that.data.promotionId,
      promotionTitle: that.data.promotionTitle,
    });
    wx.navigateBack({

    })
  },
})